package com.soho.mdi;

import java.awt.Container;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.DefaultDesktopManager;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;

public class CustomDesktopManager extends DefaultDesktopManager {
	private Rectangle start, last;
	private boolean first = true;

	// dragging ...

	public void beginDraggingFrame(JComponent frame) {
	initializeOutline(frame);
	}
	public void dragFrame(JComponent frame, int x, int y) {
	updateOutline(frame, x, y, start.width, start.height);
	}
	public void endDraggingFrame(JComponent frame) {
	endOutline(frame);
	}

	// resizing ...

	public void beginResizingFrame(JComponent frame, int dir) {
	initializeOutline(frame);
	}
	public void resizeFrame(JComponent frame,
	int x, int y, int w, int h) {
	updateOutline(frame, x, y, w, h);
	}
	public void endResizingFrame(JComponent frame) {
	endOutline(frame);
	}

	// outline ...

	private void initializeOutline(final JComponent frame) {
	// the call to setVisible() calls repaint, which
	// places a paint event on the event queue.
	// therefore, the effect of the setVisible() call is
	// not apparent until after this method returns

	frame.setVisible(false);
	start = frame.getBounds();
	last = new Rectangle(start);
	first = true;

	// the Runnable below paints the initial outline
	// after the repaint event spawned by setVisible() is
	// handled

	SwingUtilities.invokeLater(new Runnable() {
	public void run() {
	updateOutline(frame,start.x,start.y,
	start.width,start.height);
	}
	});
	}
	private void updateOutline(JComponent frame,
	int x, int y, int w, int h) {
	Container container = frame.getParent();
	Graphics g = container.getGraphics();

	try {
	g.setXORMode(container.getBackground());

	if( ! first) {
	g.drawRect(last.x, last.y,
	last.width-1, last.height-1);
	}
	g.drawRect(x, y, w-1, h-1);
	first = false;
	}
	finally {
	g.dispose();
	last.setBounds(x,y,w,h);
	}
	}
	private void endOutline(JComponent frame) {
	frame.setVisible(true);
	setBoundsForFrame(
	frame, last.x, last.y, last.width, last.height);
	}
	
    public void iconifyFrame(final JInternalFrame f) {
        //super.iconifyFrame(f);
        System.out.println("iconify...");
        JInternalFrame.JDesktopIcon desktopIcon;
        Container c = f.getParent();
    	JDesktopPane d = f.getDesktopPane();
    	boolean findNext = f.isSelected();
    		
            desktopIcon = f.getDesktopIcon();
            //desktopIcon.getDesktopPane().addM
            
            if(!wasIcon(f)) {
                Rectangle r = getBoundsForIconOf(f);
                desktopIcon.setBounds(r.x, r.y, r.width, r.height);
                setWasIcon(f, Boolean.TRUE);
            }      
            
            desktopIcon.addMouseListener(new MouseListener(){

				public void mouseClicked(MouseEvent e) {
					System.out.println("Mouse clicked..");
					deiconifyFrame(f);
				}

				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}

				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}

				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}

				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
            	
            });
            
            c.remove(f);
            c.add(desktopIcon);
            c.repaint(f.getX(), f.getY(), f.getWidth(), f.getHeight());
            
    }
    
    public void deiconifyFrame(JInternalFrame f) {
        super.deiconifyFrame(f);
        System.out.println("deiconifyFrame...");
    }    
}
